/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdAsignaSistemas.java
 *
 * Created on 16/08/2010, 05:05:22 AM
 */

package gestionseguridadadministrativacliente;

import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import seguridad.administracion.ejb30.TO.SistemaTO;
import seguridad.administracion.ejb30.entity.Sistema;

/**
 *
 * @author Administrador
 */
public class jdAsignaSistemas extends javax.swing.JDialog implements ActionListener{

    /** Creates new form jdAsignaSistemas */
    private List sistemas_check;
    private JButton jbAceptar,jbSalir;
    private String id_usuario;
    private List<SistemaTO> sistemas;
    public jdAsignaSistemas(java.awt.Frame parent, boolean modal,String id_usuario) {
        super(parent, modal);
        this.id_usuario = id_usuario;
        creaComponentes();
        try {
            cargaListaSistemas();
        } catch (Exception ex) {
            Logger.getLogger(jdAsignaSistemas.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
    }

    public void creaComponentes(){
        JPanel p1 = new JPanel();
        jbAceptar = new JButton("Guardar");
        jbSalir = new JButton("Cancelar");

        p1.add(jbAceptar);
        p1.add(jbSalir);
        add(p1, BorderLayout.SOUTH);
         
        jbAceptar.addActionListener(this);
        jbSalir.addActionListener(this);
    }


    public void cargaListaSistemas() throws Exception{
        List<SistemaTO> sistemas_de_usuario = GestionSeguridadDelegate.getInstance().getListaSistemasUsuario(this.id_usuario);

        sistemas_check = new ArrayList(1);
         sistemas =  GestionSeguridadDelegate.getInstance().getListaSistema();

        JPanel panel_checks = new JPanel(new GridLayout(sistemas.size(),1));
        for(SistemaTO si : sistemas){
            JCheckBox chk1 = new JCheckBox(si.getDescripcion());
            panel_checks.add(chk1);
            sistemas_check.add(chk1);
            for(SistemaTO si_de_usuarios : sistemas_de_usuario){
                if(si_de_usuarios.getCodigo()== si.getCodigo()){
                    chk1.setSelected(true);
                    break;
                }
            }
        }
        add(panel_checks, BorderLayout.CENTER);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdAsignaSistemas.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */

    public void guardarSistemas() throws Exception{
        List<SistemaTO> sistemas_selected = new ArrayList(1);
        int indice = 0;
         for(Object si : sistemas_check){
            JCheckBox tmp = (JCheckBox) si;
            if(tmp.isSelected()){
                sistemas_selected.add(sistemas.get(indice));
 
            }
            indice++;
        }
         if(GestionSeguridadDelegate.getInstance().setSistemasUsuario(sistemas_selected, id_usuario)){
             Utiles_MensajesDialogo.mensajeExito();
             this.dispose();
         }else
             Utiles_MensajesDialogo.mensajeError("Ocurrio un error al efectuar la operacion");
         
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(this.jbAceptar)){
            try {
                this.guardarSistemas();
            } catch (Exception ex) {
                Logger.getLogger(jdAsignaSistemas.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if(e.getSource().equals(this.jbSalir)){
            this.dispose();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
